# -*- coding:UTF-8 -*-
from bs4 import BeautifulSoup
import subprocess as sp
import requests
import random
import re

"""
函数说明:获取IP代理
"""
def get_proxys(page = 1):
    S = requests.Session()
    #西祠代理高匿IP地址
    target_url = 'http://www.xicidaili.com/nn/{}'.format(page)
    target_headers = {'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Referer':'http://www.xicidaili.com/nn/',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept-Language':'zh-CN,zh;q=0.8',
    }
    html1 = S.get(url = target_url, headers = target_headers).content.decode('utf-8')
    soup = BeautifulSoup(html1, 'html.parser')
    ips = soup.find_all('tr')
    ip_list = []
    for i in range(1, len(ips)):
        ip_info = ips[i]
        tds = ip_info.find_all('td')
        ip_list.append(tds[1].text + ':' + tds[2].text)
    return ip_list

"""
函数说明:检测IP代理
"""

def test_proxy_request(proxy):
    proxy_data = {
        'http': 'http://' + proxy ,
        'https': 'http://' + proxy ,
    }

    head_data = {
        'User_Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
        'Connection': 'keep-alive',
    }

    try:
        # http://icanhazip.com提供返回当前外网IP的服务
        response = requests.get('http://icanhazip.com', headers=head_data, proxies=proxy_data)
        outer_ip = response.text.strip().replace('\n', '')
        test_ip=re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", proxy)
        return outer_ip == ''.join(test_ip)
    except:
        return False

if __name__ == '__main__':
    proxys_list = get_proxys(1)
    flag=True
    while flag:
        for proxy in proxys_list:
            test_result = test_proxy_request(proxy)
            if test_result:
                print("IP代理成功 ==> {}".format(proxy))
                break
            else:
                print("IP代理失败 ==> {}".format(proxy))
                proxys_list.remove(proxy)
        flag=False
        print("使用代理:", proxy)